package br.com.devteam.sgap.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@NamedQueries({
	@NamedQuery(name = Usuario.QUERY_LIST_BY_LOGIN, 
			query = "select u from Usuario u where u.login = :login"),
	@NamedQuery(name = Usuario.QUERY_LIST_ASC_ORDER,
			query = "select u from Usuario u order by nome"),
	@NamedQuery(name = Usuario.QUERY_SEARCH,
			query = "select u from Usuario u where lower(u.nome) like lower(:busca)"),
			@NamedQuery(name = Usuario.QUERY_EMAIL,
			query = "select u from Usuario u where u.email = :email"),
})
@Entity
@Table
public class Usuario {
	
	public static final String QUERY_LIST_BY_LOGIN = "Usuario.listByLogin";
	public static final String QUERY_LIST_ASC_ORDER = "Usuario.listAscOrder";
	public static final String QUERY_SEARCH = "Usuario.search";
	public static final String QUERY_EMAIL = "Usuario.email";

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column
	private Integer id;
	
	@Column(nullable = false)
	private String nome;
	
	@Column(nullable = true, unique = true)
	private String email;
	
	@Column(nullable = false, unique = true)
	private String login;
	
	@Column(nullable = false)
	private String senha;
	
	@Column(nullable = false)
	private Boolean enable;
	
	@OneToMany(mappedBy = "usuario", fetch = FetchType.EAGER, orphanRemoval = true)
	@Cascade(value = {CascadeType.ALL})
	private List<Perfil> perfis;
	
	public Usuario() {
		
	}

	public void ativarUsuario() {
		enable = true;
	}
	
	public void destivarUsuario() {
		enable = false;
	}
	
	public String getPerfisToString() {
		if (getPerfis() != null){
			
			List<String> perfis = new ArrayList<String>();
			for (Perfil perfil : getPerfis()) {
				perfis.add(Perfil.getPerfilName(perfil.getPerfil()));
			}
			return perfis.toString();
		}
		
		return "";
	}
	
	public String getEnableToString() {
		return getEnable() ? "SIM" : "NÃO";
	}
	
	/**Getters and Setters*/
	/***/
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getLogin() {
		return login;
	}
	
	public void setLogin(String login) {
		this.login = login;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}
	
	public Boolean getEnable() {
		return enable;
	}
	
	public void setEnable(Boolean enable) {
		this.enable = enable;
	}

	public List<Perfil> getPerfis() {
		return perfis;
	}

	public void setPerfis(List<Perfil> perfis) {
		this.perfis = perfis;
	}
	
}
